import React, { Component } from 'react'
// import PropTypes from 'prop-types'
import tplRender from './index.jsx'
import { connect } from 'react-redux'
import { findMatchingItem } from 'app/core/menu'
class BlockTitle extends Component {
    constructor(props) {
        super(props);
        this.state = {
            list: [],
            loading: false,
            titleInfo: {}
        }
    }
    render() {
        return tplRender.call(this)
    }
    componentDidMount() {
        this.setAutoTitle(this.props, 'did')
    }
    componentWillReceiveProps(nextProps) {
        this.setAutoTitle(nextProps, 'next')
    }
    setAutoTitle(props, type) {
        const { autoTitle, autoPageActionName } = props
        if (autoTitle) {
            const { router } = this.context
            let pathname = router.history.location.pathname
            if (/\/add$/.test(pathname)) {
                pathname = pathname.replace('/add', '');
            }
            const matchingItem = findMatchingItem(pathname, props.appPermissions) || {}
            const { menuName, icon, titleIcon } = matchingItem
            this.setState({
                titleInfo: {
                    title: (menuName||'') + (autoPageActionName? ' - ' + autoPageActionName : ''),
                    icon: titleIcon || icon
                }
            })
        }
    }
    back() {
        const { router } = this.context
        const { backUrl } = this.props
        backUrl && router.history.replace(backUrl)
    }
}
function mapStateToProps(state) {
    return {
        appPermissions: state.appPermissions
    }
}
// BlockTitle.propTypes = {
//     title: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
//     showMore: PropTypes.bool,
//     children: PropTypes.node,
//     actionDom: PropTypes.node,
//     autoTitle: PropTypes.bool,
//     autoPageActionName: PropTypes.string, // 针对类似add的页面添加的额外的标题
//     backUrl: PropTypes.string, // 右侧添加返回按钮，并跳转到backUrl指定的路由
// };

// BlockTitle.contextTypes = {
//     router: PropTypes.object.isRequired
// }

export default connect(mapStateToProps, null)(BlockTitle)
